package me.firstapp.spi.shop.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.shop.JsonProduct;
import me.firstapp.common.module.shop.JsonProductInManage;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.shop.ProductService;

@Service
public class ProductServiceImpl extends BaseServiceImpl implements ProductService {

	@Override
	public PageObject<JsonProductInManage> findProductPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Long startTime, Long endTime) {
		PageObject<JsonProductInManage> result = new PageObject<JsonProductInManage>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			parameters.put("startTime", startTime);
			parameters.put("endTime", endTime);
			JSONObject jsonObject = apiBody(parameters, "/product/findProductPageByParams.do", "分页查询商品", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonProductInManage> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonProductInManage> productList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonProduct _product = JSONObject.toJavaObject(_jsonObject, JsonProduct.class);
								JsonProductInManage product = new JsonProductInManage(_product);
								productList.add(product);
							}
						}
					}
					jsonPage.setList(productList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonProduct> addProduct(Long categoryId, String listImage, String smallImage, String images,
			String name, String summary, String detail, String deliver, Double deliverFee, Integer sort) {
		SingleObject<JsonProduct> result = new SingleObject<>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("listImage", listImage);
			params.put("smallImage", smallImage);
			params.put("images", images);
			params.put("summary", summary);
			params.put("detail", detail);
			params.put("summary", summary);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("categoryId", categoryId);
			parameters.put("name", name);
			parameters.put("deliver", deliver);
			parameters.put("deliverFee", deliverFee);
			parameters.put("sort", sort);

			JSONObject jsonObject = apiBody(parameters, "/product/addProduct.do", "新增商品", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonProduct product = JSONObject.toJavaObject(jsonObject.getJSONObject("object"),
						JsonProduct.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(product);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonProduct> updateProduct(Long productId, Long categoryId, String listImage,
			String smallImage, String images, String name, String summary, String detail, String deliver,
			Double deliverFee, Integer sort, Integer disable) {
		SingleObject<JsonProduct> result = new SingleObject<>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("listImage", listImage);
			params.put("smallImage", smallImage);
			params.put("images", images);
			params.put("summary", summary);
			params.put("detail", detail);
			params.put("summary", summary);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productId", productId);
			parameters.put("categoryId", categoryId);
			parameters.put("name", name);
			parameters.put("deliver", deliver);
			parameters.put("deliverFee", deliverFee);
			parameters.put("sort", sort);
			parameters.put("disable", disable);

			JSONObject jsonObject = apiBody(parameters, "/product/updateProduct.do", "更新商品", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonProduct product = JSONObject.toJavaObject(jsonObject.getJSONObject("object"),
						JsonProduct.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(product);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteProducts(String ids) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productIds", ids);

			JSONObject resultJson = api(parameters, "/product/deleteProducts.do", HttpMethod.POST, "删除商品");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonProduct> findProductById(Long productId) {
		SingleObject<JsonProduct> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("productId", productId);
			JSONObject resultJson = api(parameters, "/product/findProductById.do", HttpMethod.GET, "通过id查询商品");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonProduct product = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonProduct.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(product);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonProduct> findProductPage(Integer pageNo, Integer pageSize, Long categoryId,
			Integer disable) {
		PageObject<JsonProduct> result = new PageObject<JsonProduct>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("categoryId", categoryId);
			parameters.put("disable", disable);
			JSONObject resultObject = apiBody(parameters, "/product/findProductsPage.do", "分页查询商品", null);

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject pageJsonObject = resultObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonProduct> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonProduct> productList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonProduct product = JSONObject.toJavaObject(_jsonObject, JsonProduct.class);
								productList.add(product);
							}
						}
					}
					jsonPage.setList(productList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public ListObject<JsonProduct> findProducts(Integer disable) {
		ListObject<JsonProduct> result = new ListObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("disable", disable);
			JSONObject resultJson = api(parameters, "/product/findProducts.do", HttpMethod.GET, "查询商品列表");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JSONArray jsonArray = resultJson.getJSONArray("items");
				List<JsonProduct> list = new ArrayList<>();
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						if (jsonObject != null) {
							JsonProduct product = JSONObject.toJavaObject(jsonObject, JsonProduct.class);
							list.add(product);
						}
					}
				}
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setItems(list);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
